Kafka Integration Documentation
This document outlines the process and architecture for the asynchronous communication implemented between the bigkart_admin and bigkart_customer services using Apache Kafka.
Architecture Overview
The system uses a Producer-Consumer pattern where the admin application generates events (incrementing a number) and the customer application consumes and displays these events.
sequenceDiagram
participant AdminApp as bigkart_admin (Port 8080)
participant Kafka as Kafka Broker (Port 9092)
participant CustomerApp as bigkart_customer (Port 8081)
Note over AdminApp: User clicks "Increment"
AdminApp->>Kafka: Publish incremented number to "increment-topic"
Kafka-->>CustomerApp: Consume message from "increment-topic"
Note over CustomerApp: Update internal state with new number
loop Every 1 second
CustomerApp->>CustomerApp: Frontend JS polls /latest-number
CustomerApp-->>CustomerApp: Update UI with latest number
end
Implementation Details
1. Dependencies
Both bigkart_admin and bigkart_customer rely on the following key dependencies added to their respective pom.xml files:
spring-kafka: For Spring Boot Kafka integration.
2. Admin Service (bigkart_admin)
The admin service acts as the Producer. It runs on the default port 8080.
-
Properties (
application.properties): Configuredspring.kafka.producer.bootstrap-servers=localhost:9092to connect to the Kafka broker. -
Service (
KafkaProducerService.java): UtilizesKafkaTemplateto send the incremented number as a string to theincrement-topic. -
Controller (
AdminKafkaController.java): Handles the HTTP GET request for the homepage and the POST request to increment the number and trigger the Kafka service. -
Frontend (
admin-kafka.jsp): Provides a simple UI with a button to increment and transmit the number.
3. Customer Service (bigkart_customer)
The customer service acts as the Consumer. It is configured to run on port 8081.
-
Properties (
application.properties): Configured to connect to Kafka, listen under the consumer groupbigkart-customer-group, and run onserver.port=8081. -
Service (
KafkaConsumerService.java): Uses@KafkaListenerto listen to messages from theincrement-topic. It parses the incoming string and updates its internal state representing the latest number. -
Controller (
CustomerController.java): Exposes a/latest-numberendpoint that returns the current number stored in the service. -
Frontend (
customer-kafka.jsp): Uses JavaScriptfetchto poll the/latest-numberendpoint every 1 second and dynamically update the DOM with the new value, including a brief CSS animation.
How to Run and Test
-
Start Kafka: Ensure the Kafka Broker is running locally on port
9092. -
Start Admin App: Run the
bigkart_adminSpring Boot application. It will be accessible athttp://localhost:8080. -
Start Customer App: Run the
bigkart_customerSpring Boot application. It will be accessible athttp://localhost:8081. -
Test Flow:
- Open both URLs in separate browser windows.
- Click the "Increment & Send" button in the admin window.
- Observe the number automatically updating in the customer window.